package com.zmc.member.infrastructure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zmc.member.infrastructure.bean.vo.MemberVo;
import com.zmc.member.infrastructure.entity.MemberEntity;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zmc.member.infrastructure.dao.MemberDao;
import com.zmc.member.infrastructure.service.MemberService;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author zmc
 */
@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Override
    public MemberEntity getByPhone(String phone) {
        LambdaQueryWrapper<MemberEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberEntity::getMobile, phone);
        return getOne(wrapper);
    }

    @Override
    public MemberEntity getByMemberId(String memberId) {
        LambdaQueryWrapper<MemberEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberEntity::getMemberId, memberId)
                .last(" limit 1");
        return getOne(wrapper);
    }

    @Override
    @Transactional
    public void updateMember(String memberId, MemberVo vo) {
        LambdaUpdateWrapper<MemberEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(MemberEntity::getMemberId, memberId)
                .set(MemberEntity::getAvatar, vo.getAvatar())
                .set(MemberEntity::getUsername, vo.getUsername())
                .set(MemberEntity::getNickname, vo.getNickname())
                .set(MemberEntity::getEmail, vo.getEmail())
                .set(MemberEntity::getJob, vo.getJob())
                .set(MemberEntity::getSign, vo.getSign())
                .set(MemberEntity::getBirth, vo.getBirth())
                .set(MemberEntity::getGender, vo.getGender());
        update(wrapper);
    }

    @Override
    @Transactional
    public void updatePhone(String memberId, String phone) {
        LambdaUpdateWrapper<MemberEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(MemberEntity::getMemberId, memberId)
                .set(MemberEntity::getMobile, phone);
        update(wrapper);
    }

    @Override
    @Transactional
    public void updatePassword(String memberId, String salt, String password) {
        LambdaUpdateWrapper<MemberEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(MemberEntity::getMemberId, memberId)
                .set(MemberEntity::getSalt, salt)
                .set(MemberEntity::getPassword, password);
        update(wrapper);
    }
}